#include <string>
#include <vector>
#include <iostream>

// 单词反转
// 解法1：暴力解法
namespace S1 {
    class Solution {
    public:
        std::string reverseWords(std::string& s) {
            auto strs = splitBySpace(s);

            std::string result;
            for (int i = strs.size() - 1; i >= 0; i--) {
                result += strs[i];
                result += " ";
            }
            return result;
        }

    private:
        std::vector<std::string> splitBySpace(const std::string& str) {
            std::vector<std::string> result;
            size_t start = 0;
            size_t end = str.find(' ');

            while (end != std::string::npos) {
                if (end > start) {
                    result.push_back(str.substr(start, end - start));
                }
                start = end + 1;
                end = str.find(' ', start);
            }

            if (start < str.size()) {
                result.push_back(str.substr(start));
            }

            return result;
        }
    };
}

// 双指针解法
namespace S2 {
class Solution {
    public:
        std::string reverseWords(std::string& s) {
            
        }
    };
}

int main() {
    S1::Solution s1;
    std::string s = " hello world ";
    auto result = s1.reverseWords(s);

    std::cout << result << std::endl;
}